package StackLeetCode;

import java.util.Stack;

public class IsStackSequence {

    /**
     * 输入：
     * [1,2,3,4,5],[4,5,3,2,1]
     * 复制
     * 返回值：
     * true
     * 复制
     * 说明：
     * 可以通过push(1)=>push(2)=>push(3)=>push(4)=>pop()=>push(5)=>pop()=>pop()=>pop()=>pop()
     * 这样的顺序得到[4,5,3,2,1]这个序列，返回true
     * @param args
     */

    public static void main(String[] args) {
        int[] push = {1,2,3,4,5};
        int[] pop = {4,5,3,1,2};
        System.out.println(isPopOrder(push, pop));

    }
    public static boolean isPopOrder (int[] pu, int[] po) {
        if (po.length == 0 || pu.length == 0) {
            return false;
        }
        Stack<Integer> stack = new Stack<>();
        int j = 0;
        for (int i = 0; i < pu.length; i++) {

            stack.push(pu[i]);
            while (j<po.length && !stack.empty() && stack.peek() == po[j]) {
                stack.pop();
                j++;
            }

        }
        return stack.empty();
    }
}
